<?php
namespace app\admin\model;

use think\Model;
use think\Config;

class Category extends Model
{
    //连接趁早数据库
    protected $connection = 'cz_db_config';

    /**
     * 根据单字段获取信息
     * @param $user_name
     * @param string $fields
     * @return object
     */
    public function getInfoByField($field_name, $field_value, $fields = '*')
    {
        return $this->where($field_name, $field_value)->field($fields)->find();
    }

    /**
     * 根据分类ID获取分类名
     * @param $ids
     * @return string
     */
    public function getNameById($ids)
    {
        $names = '';
        if (is_array($ids)) {
            $names_ary = $this->where([
                'id' => ['in', implode(',', $ids)]
            ])->column('name');
            $names = $names_ary ? implode(',', $names_ary) : '';
        } else {
            $names = $this->where([
                'id' => $ids
            ])->value('name');
        }
        return $names;
    }

    /**
     * 获取所有分类
     * @return array
     */
    public function getAllLists()
    {
        return $this->where([
            'type' => 2
        ])->field('id, name as classify_name')->select();
    }
}